package com.kehongborui.fliggy.queue;

import com.kehongborui.fliggy.entity.Log;
import com.kehongborui.fliggy.util.JedisUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-09-10
 */
@Component
public class LogsQueue {
    Logger log = Logger.getLogger(LogsQueue.class);

    @Autowired
    private JedisUtil jedisUtil;
//    private static Class lock = LogsQueue.class;

    // 日志队列名称
    private static final String KEY = "logs_list";

    /**
     * 向video_url_queue放入数据
     * @param o
     */
    public void push(Log o) {
        synchronized (jedisUtil) {  // 调用notify 的地方 锁对象必须存在
            jedisUtil.lPush(KEY, o);
            jedisUtil.notify();
        }
    }

    /**
     * 获取当前队列的长度
     * @return
     */
    public long length() {
        return jedisUtil.len(KEY);
    }

    public Log pop() {
        synchronized (jedisUtil) {
            if (length() == 0) {
                try {
                    jedisUtil.wait();
                } catch (InterruptedException e) {
                    jedisUtil.notify();
                    log.info(e.getMessage());
                    e.printStackTrace();
                }
            }
        }


        return jedisUtil.rPop(KEY, Log.class);
    }
}